package com.yaodian100.mall.member.web;

import java.math.BigDecimal;
import java.util.Hashtable;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.common.utils.ServerValue;
import com.yaodian100.ec.category.service.CategoryService;
import com.yaodian100.ec.member.entity.Member;
import com.yaodian100.ec.member.entity.ViewWishList;
import com.yaodian100.ec.member.entity.WishList;
import com.yaodian100.ec.member.service.MemberService;
import com.yaodian100.mall.member.util.UserData;

public class CollectionAction extends ActionSupport {

	private static final long serialVersionUID = -1100102087605481981L;
	private MemberService memberService;
	private CategoryService categoryService;
	private Member member;
	private List<ViewWishList> viewWishList;
	private String productId;
	private String categoryId;
	private String categoryMemo;
	private String productPrice;
	private String actionResult;
	private String wishListId;
	private String delIdList;
	private int productListSize;
	private int categoryListSize;
	private Hashtable<Long, String> categoryH;

	/**
	 * 收藏商品
	 * 
	 * @return
	 * @throws Exception
	 */
	public String toproductc() throws Exception {
		if (isLogin()) {
			CommonCriteria crit = new CommonCriteria();
			crit.addEq("memberId", member.getMemberId());
			crit.addEq("categoryId", null);
			crit.addNe("shelfPrice", null);
			crit.addNe("is_delete", "Y");
			viewWishList = memberService.getViewWishLists(0, -1, crit, null);
			productListSize = viewWishList.size();
			crit = new CommonCriteria();
			crit.addEq("memberId", member.getMemberId());
			crit.addEq("productId", null);
			List<ViewWishList> tmp = memberService.getViewWishLists(0, -1, crit, null);
			categoryListSize = tmp.size();
		}
		return "toproductcollection";
	}

	/**
	 * 收藏类别
	 * 
	 * @return
	 * @throws Exception
	 */
	public String tocategoriesc() throws Exception {
		if (isLogin()) {
			CommonCriteria crit = new CommonCriteria();
			crit.addEq("memberId", member.getMemberId());
			crit.addEq("productId", null);
			viewWishList = memberService.getViewWishLists(0, -1, crit, null);
			categoryListSize = viewWishList.size();
			crit = new CommonCriteria();
			crit.addEq("memberId", member.getMemberId());
			crit.addEq("categoryId", null);
			crit.addNe("shelfPrice", null);
			List<ViewWishList> tmp = memberService.getViewWishLists(0, -1, crit, null);
			productListSize = tmp.size();
		}
		return "tocategoriescollection";
	}

	/**
	 * 儲存or更新收藏
	 * 
	 * @return
	 * @throws Exception
	 */
	public String saveWishList() throws Exception {
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(
				ServletActionContext.HTTP_RESPONSE);
		UserData userData = new UserData(request, response);
		member = memberService.getMemberByLoginId(userData.getLoginId(), request.getServerName());
		WishList entity;
		if (wishListId == null) {
			entity = new WishList();
			entity.setMemberId(member.getMemberId());
			if (productId != null) {
				entity.setProductId(productId);
				entity.setPutProductPrice(new BigDecimal(productPrice));
				entity.setFullUrl(ServerValue.getFrontContextPath() + "/product/product.do?productId=" + productId);
			}
			if (categoryId != null) {
				Long cId = Long.parseLong(categoryId);
				entity.setCategoryId(cId);
				entity.setCategoryPath(categoryService.makeCategorieURL(cId, true));
			}
			if (memberService.checkWishListExist(entity)) {
				actionResult = "exists";
				return "wishlistsaved";
			}
		} else {
			entity = memberService.getWishList(Integer.parseInt(wishListId));
		}
		entity.setMemo(categoryMemo);
		entity.setIsPriceNotify("N");
		entity.setIsRestockNotify("N");
		entity.setLowerPriceNotify(new BigDecimal("0.0"));
		if (null != wishListId) {
			entity = memberService.updateWishList(entity);
		} else {
			entity = memberService.saveWishList(entity);
		}

		if (entity != null) {
			actionResult = "success";
		} else {
			actionResult = "error";
		}
		return "wishlistsaved";
	}

	/**
	 * 儲存貨到通知
	 * 
	 * @return
	 * @throws Exception
	 */
	public String saveRestockRemind() throws Exception {
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(
				ServletActionContext.HTTP_RESPONSE);
		UserData userData = new UserData(request, response);
		member = memberService.getMemberByLoginId(userData.getLoginId(), request.getServerName());
		WishList entity;
		if (wishListId == null) {
			if (StringUtils.isBlank(productId)) {
				actionResult = "error";
				return "restockRemind";
			}
			entity = new WishList();
			entity.setMemberId(member.getMemberId());
			entity.setProductId(productId);
			entity.setPutProductPrice(new BigDecimal(productPrice));
			entity.setFullUrl(ServerValue.getFrontContextPath() + "/product/product.do?productId=" + productId);
			entity.setMemo("");
			entity.setIsPriceNotify("N");
			entity.setLowerPriceNotify(new BigDecimal("0.0"));
			WishList tmp = memberService.getWishList(entity);
			if (null != tmp) {
				if ("Y".equalsIgnoreCase(tmp.getIsRestockNotify())) {
					actionResult = "exists";
					return "restockRemind";
				}
				entity = tmp;
				wishListId = Integer.toString(entity.getId());
			}
		} else {
			entity = memberService.getWishList(Integer.parseInt(wishListId));
		}
		entity.setIsRestockNotify("Y");

		if (StringUtils.isNotBlank(wishListId)) {
			entity = memberService.updateWishList(entity);
		} else {
			entity = memberService.saveWishList(entity);
		}
		if (entity != null) {
			actionResult = "success";
			return "restockRemind";
		}
		actionResult = "error";
		return "restockRemind";
	}

	/**
	 * 取消貨到通知
	 * 
	 * @return
	 * @throws Exception
	 */
	public String cancelRestockRemind() throws Exception {
		if (StringUtils.isBlank(wishListId)) {
			actionResult = "error";
			return "restockRemind";
		}
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(
				ServletActionContext.HTTP_RESPONSE);
		UserData userData = new UserData(request, response);
		member = memberService.getMemberByLoginId(userData.getLoginId(), request.getServerName());
		WishList entity = memberService.getWishList(Integer.parseInt(wishListId));
		entity.setIsRestockNotify("N");
		entity = memberService.updateWishList(entity);
		if (entity != null) {
			actionResult = "success";
			return "restockRemind";
		}
		actionResult = "error";
		return "restockRemind";
	}

	/**
	 * 检查收藏类别是否已存在
	 * 
	 * @return
	 * @throws Exception
	 */
	public String checkWishList() throws Exception {
		if (wishListId == null && categoryId != null) {
			HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(
					ServletActionContext.HTTP_REQUEST);
			HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(
					ServletActionContext.HTTP_RESPONSE);
			UserData userData = new UserData(request, response);
			member = memberService.getMemberByLoginId(userData.getLoginId(), request.getServerName());
			WishList entity;
			entity = new WishList();
			entity.setMemberId(member.getMemberId());
			Long cId = Long.parseLong(categoryId);
			entity.setCategoryId(cId);

			if (memberService.checkWishListExist(entity)) {
				actionResult = "exists";
				return "wishlistsaved";
			}
		}
		actionResult = "ok";
		return "wishlistsaved";
	}

	/**
	 * 刪除wishList
	 * 
	 * @return
	 * @throws Exception
	 */
	public String deleteWishList() throws Exception {
		String[] delList = delIdList.split(",");
		if (memberService.deleteWishList(delList)) {
			actionResult = "success";
		} else {
			actionResult = "error";
		}
		return "wishlistdeleted";
	}

	private boolean isLogin() throws Exception {
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(
				ServletActionContext.HTTP_RESPONSE);
		UserData userData = new UserData(request, response);
		if (StringUtils.isNotBlank(userData.getLoginId())) {
			member = memberService.getMemberByLoginId(userData.getLoginId(), request.getServerName());
		} else {
			return false;
		}
		if (member == null) {
			return false;
		}
		return true;
	}

	public MemberService getMemberService() {
		return memberService;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public List<ViewWishList> getViewWishList() {
		return viewWishList;
	}

	public void setViewWishList(List<ViewWishList> viewWishList) {
		this.viewWishList = viewWishList;
	}

	public Member getMember() {
		return member;
	}

	public void setMember(Member member) {
		this.member = member;
	}

	public String getProductId() {
		return productId;
	}

	public void setProductId(String productId) {
		this.productId = productId;
	}

	public String getProductPrice() {
		return productPrice;
	}

	public void setProductPrice(String productPrice) {
		this.productPrice = productPrice;
	}

	public String getActionResult() {
		return actionResult;
	}

	public void setActionResult(String actionResult) {
		this.actionResult = actionResult;
	}

	public int getProductListSize() {
		return productListSize;
	}

	public void setProductListSize(int productListSize) {
		this.productListSize = productListSize;
	}

	public int getCategoryListSize() {
		return categoryListSize;
	}

	public void setCategoryListSize(int categoryListSize) {
		this.categoryListSize = categoryListSize;
	}

	public CategoryService getCategoryService() {
		return categoryService;
	}

	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

	public Hashtable<Long, String> getCategoryH() {
		return categoryH;
	}

	public void setCategoryH(Hashtable<Long, String> categoryH) {
		this.categoryH = categoryH;
	}

	public String getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(String categoryId) {
		this.categoryId = categoryId;
	}

	public String getCategoryMemo() {
		return categoryMemo;
	}

	public void setCategoryMemo(String categoryMemo) {
		this.categoryMemo = categoryMemo;
	}

	public String getWishListId() {
		return wishListId;
	}

	public void setWishListId(String wishListId) {
		this.wishListId = wishListId;
	}

	public String getDelIdList() {
		return delIdList;
	}

	public void setDelIdList(String delIdList) {
		this.delIdList = delIdList;
	}
}